﻿using Microsoft.AspNetCore.Mvc;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Localization;
using Volo.Abp.Modularity;
using Volo.Abp.VirtualFileSystem;

namespace Ebusiness_Logs
{
    [DependsOn(
    typeof(AbpAspNetCoreMvcModule)
    )]
    public class LogsMoudule : AbpModule
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="context"></param>
        public override void ConfigureServices(ServiceConfigurationContext context)
        {

            IServiceCollection services = context.Services;
            IConfiguration configuration = services.GetConfiguration();
            // Add services to the container.
            //配置自定义控制器
            ConfigurControllers(context, configuration);
            //配置日志工厂模式
            ConfigLogFactory(context, configuration);
            //配置Swagger
            ConfigureSwaggerServices(context, configuration);
        }

        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            IApplicationBuilder app = context.GetApplicationBuilder();
            var env = context.GetEnvironment();
            app.UseSwagger();
            app.UseSwaggerUI();
            app.UseStaticFiles();
            //使用路由中间件
            app.UseRouting();
            //身份鉴权中间件
            app.UseAuthorization();
            app.UseConfiguredEndpoints(); //执行Abp Action
        }

        /// <summary>
        /// 配置自定义控制器
        /// </summary>
        public void ConfigurControllers(ServiceConfigurationContext context, IConfiguration configuration)
        {
            //配置自定义过滤器
            context.Services.AddControllers(option =>
            {
                //全局取消默认token校验 忽略xss攻击
                option.Filters.Add(new IgnoreAntiforgeryTokenAttribute());
            });
            //servicesout configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            context.Services.AddEndpointsApiExplorer();
        }

        /// <summary>
        /// 配置日志工厂模式
        /// </summary>
        public void ConfigLogFactory(ServiceConfigurationContext context, IConfiguration configuration)
        {
            ////配置日志工厂
            //context.Services.AddLogging(x =>
            //{
            //    x.AddConfiguration(configuration.GetSection("Logging"));
            //    x.AddLog4Net("log4Net.config");
            //    x.AddConsole();
            //});
        }

        /// <summary>
        /// 配置Swagger
        /// </summary>
        /// <param name="context"></param>
        /// <param name="configuration"></param>
        public void ConfigureSwaggerServices(ServiceConfigurationContext context, IConfiguration configuration)
        {
            //不带身份验证注册
            context.Services.AddSwaggerGen();
        }
    }
}
